﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ActiveUp.Net.Mail;
using ActiveUp.Net;

namespace Microhard.Core.Mail
{
    public static class MailTool
    {
        public static void SendHTMLMailWithSSL(string fromEmail, string toEmail, string subject, string htmlBody, string smtp, string port, string loginUser, string pwd)
        {
            // We create the message object
            Message message = new ActiveUp.Net.Mail.Message();

            // We assign the sender email
            message.From.Email = fromEmail;

            // We assign the recipient email
            message.To.Add(toEmail);

            // We assign the subject
            message.Subject = "=?UTF-8?B?" + Convert.ToBase64String(Encoding.UTF8.GetBytes(subject)) + "?=";

            // We assign the body text
            message.BodyHtml.Charset = "utf-8";
            message.BodyHtml.Text = htmlBody;


            try
            {
                SmtpClient.SendSsl(message, smtp, Convert.ToInt32(port), loginUser, pwd, SaslMechanism.Login);

                Log.Info("SendMailWithSMTP", toEmail + "发送成功");
            }
            catch (SmtpException ex)
            {
                Log.Error("SendMailWithSMTP", ex);
            }
            catch (Exception ex)
            {
                Log.Error("SendMailWithSMTP", ex);
            }
        }

        public static void SendMailWithSMTP(string fromEmail, string toEmail, string subject, string body, string smtp, string port, string loginUser, string pwd)
        {
            // We create the message object
            Message message = new ActiveUp.Net.Mail.Message();

            // We assign the sender email
            message.From.Email = fromEmail;

            // We assign the recipient email
            message.To.Add(toEmail);

            // We assign the subject
            message.Subject = "=?UTF-8?B?" + Convert.ToBase64String(Encoding.UTF8.GetBytes(subject)) + "?=";

            // We assign the body text
            message.BodyText.Charset = "utf-8";
            message.BodyText.Text = body;


            try
            {
                SmtpClient.SendSsl(message, smtp, Convert.ToInt32(port), loginUser, pwd, SaslMechanism.Login);
                Log.Info("SendMailWithSMTP", toEmail + "发送成功");
            }
            catch (SmtpException ex)
            {
                Log.Error("SendMailWithSMTP", ex);
            }
            catch (Exception ex)
            {
                Log.Error("SendMailWithSMTP", ex);
            }

        }
    }
}
